package net.tngou.iask.service;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import net.tngou.iask.dao.StatisticDao;
import net.tngou.iask.entity.Statistic;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Agerange;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.User;
import net.tngou.iask.util.DateUtil;

/**
 * 统计
 * @author tngou@tngou.net
 *
 */
public class StatisticService extends BaseService {

	
	
	/**
	 * 取得患者统计 总数 月新增加 性别 等
	 * @param main
	 * @return
	 */
	public Statistic getPatient(long main) {
		Statistic statistic = new Statistic();
		StatisticDao dao = new StatisticDao();	
		int total=dao.patientTotal(main);
		int	male=dao.patientGender(main, 1);
		int	female=dao.patientGender(main, 2);
		int othersex=total-male-female;
		int month =dao.patientDate(main, 30);
		int month_last=dao.patientDate(main, 60)-month;
		int week =dao.patientDate(main, 7);
		int week_last=dao.patientDate(main, 14)-week;
		int day =dao.patientDate(main, 1);
		int day_last=dao.patientDate(main, 2)-day;
		statistic.setPatient_total(total);
		statistic.setPatient_male(male);
		statistic.setPatient_female(female);
		statistic.setPatient_othersex(othersex);
		statistic.setPatient_month(month);
		statistic.setPatient_month_last(month_last);
		statistic.setPatient_week(week);
		statistic.setPatient_week_last(week_last);
		statistic.setPatient_day(day);
		statistic.setPatient_day_last(day_last);
		return statistic;
		
	}
	
	/**
	 * 取得年龄段
	 * @param main
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Statistic> getPatientAge(long main) {
		List<Statistic> list = new ArrayList<Statistic>();
		List<Agerange> ls = (List<Agerange>) getList(Agerange.class);
		StatisticDao dao=new StatisticDao();
		ls.forEach(e->{
			Statistic statistic = new Statistic();
			statistic.setName(e.getName());
			statistic.setValue(dao.patientAge(main, e.getStartage(), e.getEndage()));
			list.add(statistic);
					
		});
		return list;
	}
	
	
	/**
	 * 
	 * @param main
	 * @param date
	 * @param type
	 * @return
	 */
	public List<Statistic> getPatientList(long main,int date,int type) {
		List<Statistic> list = new ArrayList<>();
		StatisticDao dao = new StatisticDao();
		switch (type) {
		case 0://日
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusDays(date-i);
				int total=dao.patientTotal(main, now, now);
				statistic.setName(DateUtil.format(now, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
			}
			break;
		case 1://周
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusWeeks(date-i);
				DayOfWeek week=now.getDayOfWeek();
				LocalDateTime start = now.plusDays(1-week.getValue());
				LocalDateTime end = now.plusDays(7-week.getValue());
				int total=dao.patientTotal(main, start, end);
				statistic.setName(DateUtil.format(start, "MM-dd")+"~"+DateUtil.format(end, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
				
			}
			break;	
		case 2://月
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusMonths(date-i);	
				LocalDateTime start = now.minusDays(now.getDayOfMonth()-1);
				LocalDateTime end = start.plusMonths(1);
				int total=dao.patientTotal(main, start, end);
				statistic.setName(now.getMonthValue()+"月");
				statistic.setValue(total);
				list.add(statistic);	
			}
			break;	
		default:
			break;
		}

		return list;
	}
	
	
	
	/**
	 * 取得 用户对应用户数
	 * @param main
	 * @return
	 */
	public Statistic getRole(long main) {
		Statistic statistic = new Statistic();
		User bean = new User();
		String filter="";
		if(main!=0)filter="main="+main+" AND ";
		
		statistic.setRole0(bean.totalCount(filter+"role=0"));
		statistic.setRole1(bean.totalCount(filter+"role=1"));
		statistic.setRole2(bean.totalCount(filter+"role=2"));
		statistic.setRole3(bean.totalCount(filter+"role=3"));
		statistic.setRole4(bean.totalCount(filter+"role=4"));
		statistic.setRole5(bean.totalCount(filter+"role=5"));
		statistic.setRole6(bean.totalCount(filter+"role=6"));
		
		return statistic;
	}
	
	/**
	 * 取得治疗情况
	 * @return
	 */
	public Statistic getCure(long main) {
		Statistic statistic = new Statistic();
		Cure bean= new Cure();
		int cure_total=0;//治疗总数

		if(main==0)
		{
			cure_total=bean.totalCount();
		}else
		{
			cure_total=bean.totalCount("main="+main);
		}
		StatisticDao dao = new StatisticDao();
		int cure_month=dao.cureDate(main,30);
		int cure_month_last=dao.cureDate(main, 60)-cure_month;
		int cure_week=dao.cureDate(main,7);
		int cure_week_last=dao.cureDate(main, 7)-cure_week;
		int cure_day=dao.cureDate(main,1);
		int cure_day_last=dao.cureDate(main, 2)-cure_day;
		int cure_place = dao.curePlace(main);
		int cure_department = dao.cureDepartment(main);
		int cure_treatment = dao.cureTreatment(main);
		
		statistic.setCure_department(cure_department);
		statistic.setCure_month(cure_month);
		statistic.setCure_week(cure_week);
		statistic.setCure_day(cure_day);
		statistic.setCure_month_last(cure_month_last);
		statistic.setCure_week_last(cure_week_last);
		statistic.setCure_day_last(cure_day_last);
		statistic.setCure_place(cure_place);
		statistic.setCure_total(cure_total);
		statistic.setCure_treatment(cure_treatment);		
		return statistic;	
	}
	
	/**
	 * 疾病治疗统计
	 * @param main
	 * @param disease
	 * @return
	 */
	public Statistic getCure(long main,long disease) {
		Statistic statistic = new Statistic();
		Cure bean= new Cure();
		int cure_total=0;//治疗总数

		if(main==0)
		{
			cure_total=bean.totalCount("disease="+disease);
		}else
		{
			cure_total=bean.totalCount("main="+main+" AND disease="+disease);
		}
		StatisticDao dao = new StatisticDao();
		int cure_month=dao.cureDate(main,disease,30);
		int cure_month_last=dao.cureDate(main, disease,60)-cure_month;
		int cure_week=dao.cureDate(main,disease,7);
		int cure_week_last=dao.cureDate(main, disease,7)-cure_week;
		int cure_day=dao.cureDate(main,disease,1);
		int cure_day_last=dao.cureDate(main,disease, 2)-cure_day;
		
		statistic.setCure_month(cure_month);
		statistic.setCure_week(cure_week);
		statistic.setCure_day(cure_day);
		statistic.setCure_month_last(cure_month_last);
		statistic.setCure_week_last(cure_week_last);
		statistic.setCure_day_last(cure_day_last);

		statistic.setCure_total(cure_total);
				
		return statistic;	
	}
	
	/**
	 * 取得治疗疾病 对应用户数
	 * @param main
	 * @return
	 */
	public List<Statistic> getCureList(long main) {
		StatisticDao dao = new StatisticDao();
		return dao.getCureList(main);
		
	}
	
	public List<Statistic> getCureList(long main,int date,int type) {
		List<Statistic> list = new ArrayList<>();
		StatisticDao dao = new StatisticDao();
		switch (type) {
		case 0://日
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusDays(date-i);
				int total=dao.cureTotal(main, now, now);
				statistic.setName(DateUtil.format(now, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
			}
			break;
		case 1://周
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusWeeks(date-i);
				DayOfWeek week=now.getDayOfWeek();
				LocalDateTime start = now.plusDays(1-week.getValue());
				LocalDateTime end = now.plusDays(7-week.getValue());
				int total=dao.cureTotal(main, start, end);
				statistic.setName(DateUtil.format(start, "MM-dd")+"~"+DateUtil.format(end, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
				
			}
			break;	
		case 2://月
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusMonths(date-i);	
				LocalDateTime start = now.minusDays(now.getDayOfMonth()-1);
				LocalDateTime end = start.plusMonths(1);
				int total=dao.cureTotal(main, start, end);
				statistic.setName(now.getMonthValue()+"月");
				statistic.setValue(total);
				list.add(statistic);	
			}
			break;	
		default:
			break;
		}

		return list;
	}
	
	/**
	 * 取得随访信息
	 * @param main
	 * @return
	 */
	public Statistic getAccess(long main) {
		Statistic statistic = new Statistic();
		Access bean = new Access();
		int access_total;//随访总数
	
		String filter="";
		if(main==0)
		{
			access_total=bean.totalCount();
		}else
		{
			filter="main="+main+" AND ";
			access_total=bean.totalCount("main="+main);
		}
		int access_state0 = bean.totalCount(filter+" state=0");
		int access_state1 = bean.totalCount(filter+" state=1");
		int access_state2 = bean.totalCount(filter+" state=2");
		int access_state3 = bean.totalCount(filter+" state=-1");
		StatisticDao dao = new StatisticDao();
		int access_month = dao.accessDate(main, 30);
		int access_week = dao.accessDate(main, 7);
		int access_day = dao.accessDate(main, 1);
		int access_month_last = dao.accessDate(main, 60)-access_month;
		int access_week_last = dao.accessDate(main, 14)-access_week;
		int access_day_last = dao.accessDate(main, 2)-access_day;
		statistic.setAccess_state0(access_state0);
		statistic.setAccess_state1(access_state1);
		statistic.setAccess_state2(access_state2);
		statistic.setAccess_state3(access_state3);
		statistic.setAccess_total(access_total==0?1:access_total);
		statistic.setAccess_day(access_day);
		statistic.setAccess_day_last(access_day_last);
		statistic.setAccess_week(access_week);
		statistic.setAccess_week_last(access_week_last);
		statistic.setAccess_month(access_month);
		statistic.setAccess_month_last(access_month_last);
		return statistic;	
	}
	
	
	/**
	 * 取得随访信息
	 * @param main
	 * @param disease
	 * @return
	 */
	public Statistic getAccess(long main,long disease) {
		Statistic statistic = new Statistic();
	
		StatisticDao dao = new StatisticDao();
		int access_total=dao.accessTotal(main, disease);//随访总数
		int access_state0 = dao.accessTotal(main, disease, 0);
		int access_state1 = dao.accessTotal(main, disease, 1);
		int access_state2 = dao.accessTotal(main, disease, 2);
		int access_state3 = dao.accessTotal(main, disease, -1);
		int access_month = dao.accessDate(main, disease,30);
		int access_week = dao.accessDate(main,disease, 7);
		int access_day = dao.accessDate(main,disease, 1);
		int access_month_last = dao.accessDate(main,disease, 60)-access_month;
		int access_week_last = dao.accessDate(main, disease,14)-access_week;
		int access_day_last = dao.accessDate(main,disease, 2)-access_day;
		statistic.setAccess_state0(access_state0);
		statistic.setAccess_state1(access_state1);
		statistic.setAccess_state2(access_state2);
		statistic.setAccess_state3(access_state3);
		statistic.setAccess_total(access_total==0?1:access_total);
		statistic.setAccess_day(access_day);
		statistic.setAccess_day_last(access_day_last);
		statistic.setAccess_week(access_week);
		statistic.setAccess_week_last(access_week_last);
		statistic.setAccess_month(access_month);
		statistic.setAccess_month_last(access_month_last);
		return statistic;	
	}
	
	/**
	 * 取得最近几天的状态
	 * @param main 
	 * @param start 计算天数 
	 * @param date 天数
	 * @param state 状态 
	 * @return
	 */
	public Statistic getAccessList(long main,int start,int date,int state) {
		Statistic statistic = new Statistic();
		StatisticDao dao = new StatisticDao();
		String name="";
		switch (state) {
		case 0:
			name="新创建";
			break;
		case 1:
			name="进行中";
			break;
		case 2:
			name="完成";
			break;
		case -1:
			name="失效";
			break;
		default:
			break;
		}
		int value=dao.getAccess(main, start, date, state);
		statistic.setName(name);
		statistic.setValue(value);
		return statistic;
		
	}
	public Statistic getAccess(long main,int start,int date) {
		Statistic statistic = new Statistic();
		StatisticDao dao = new StatisticDao();
		String name="新建";
		int value=dao.getAccess(main, start, date);
		statistic.setName(name);
		statistic.setValue(value);
		return statistic;
		
	}
	
	public List<Statistic> getAccessList(long main,int date,int type) {
		List<Statistic> list = new ArrayList<>();
		StatisticDao dao = new StatisticDao();
		switch (type) {
		case 0://日
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusDays(date-i);
				int total=dao.accessTotal(main, now, now);
				statistic.setName(DateUtil.format(now, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
			}
			break;
		case 1://周
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusWeeks(date-i);
				DayOfWeek week=now.getDayOfWeek();
				LocalDateTime start = now.plusDays(1-week.getValue());
				LocalDateTime end = now.plusDays(7-week.getValue());
				int total=dao.accessTotal(main, start, end);
				statistic.setName(DateUtil.format(start, "MM-dd")+"~"+DateUtil.format(end, "MM-dd"));
				statistic.setValue(total);
				list.add(statistic);
				
			}
			break;	
		case 2://月
			for (int i = 1; i <=date; i++) {
				Statistic statistic = new Statistic();
				LocalDateTime now = LocalDateTime.now().minusMonths(date-i);	
				LocalDateTime start = now.minusDays(now.getDayOfMonth()-1);
				LocalDateTime end = start.plusMonths(1);
				int total=dao.accessTotal(main, start, end);
				statistic.setName(now.getMonthValue()+"月");
				statistic.setValue(total);
				list.add(statistic);	
			}
			break;	
		default:
			break;
		}

		return list;
	}
	
}
